package cn.FagDegrn.chatbot.api.test;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;

public class ApiTest {
    @Test
    public void query_unanswered_question() throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpGet get = new HttpGet("https://api.zsxq.com/v2/groups/15555824285212/topics?scope=unanswered_questions&count=20");

        get.addHeader("cookie","zsxq_access_token=6CFF04BA-04CB-CAC7-EF27-EC887B200FB8_FFF0B2EB34B2481F; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22415442854184218%22%2C%22first_id%22%3A%22192d88b976429e-0ad636145e1ccb8-4c657b58-2359296-192d88b9765466%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkyZDg4Yjk3NjQyOWUtMGFkNjM2MTQ1ZTFjY2I4LTRjNjU3YjU4LTIzNTkyOTYtMTkyZDg4Yjk3NjU0NjYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiI0MTU0NDI4NTQxODQyMTgifQ%3D%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22415442854184218%22%7D%2C%22%24device_id%22%3A%22192d88b976429e-0ad636145e1ccb8-4c657b58-2359296-192d88b9765466%22%7D; zsxqsessionid=7d1dedf98b2e8c803fd6d17eae5a1e5b; abtest_env=product");
        get.addHeader("Content-Type","application/json, text/plain, */*");

        CloseableHttpResponse response = httpClient.execute(get);

        if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String res = EntityUtils.toString(response.getEntity());
            System.out.println(res);
        }else{
            System.out.println(response.getStatusLine().getStatusCode());
        }
    }

    public void question() throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost post = new HttpPost("https://api.zsxq.com/v2/topics/8858242284554282/answer");

        post.addHeader("cookie","zsxq_access_token=6CFF04BA-04CB-CAC7-EF27-EC887B200FB8_FFF0B2EB34B2481F; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22415442854184218%22%2C%22first_id%22%3A%22192d88b976429e-0ad636145e1ccb8-4c657b58-2359296-192d88b9765466%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkyZDg4Yjk3NjQyOWUtMGFkNjM2MTQ1ZTFjY2I4LTRjNjU3YjU4LTIzNTkyOTYtMTkyZDg4Yjk3NjU0NjYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiI0MTU0NDI4NTQxODQyMTgifQ%3D%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22415442854184218%22%7D%2C%22%24device_id%22%3A%22192d88b976429e-0ad636145e1ccb8-4c657b58-2359296-192d88b9765466%22%7D; zsxqsessionid=7d1dedf98b2e8c803fd6d17eae5a1e5b; abtest_env=product");
        post.addHeader("Content-Type","application/json, text/plain, */*");

        CloseableHttpResponse response = httpClient.execute(post);

        if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String res = EntityUtils.toString(response.getEntity());
            System.out.println(res);
        }else{
            System.out.println(response.getStatusLine().getStatusCode());
        }
    }

    @Test
    public void test_chatGPT() throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost post = new HttpPost("https://open.bigmodel.cn/api/paas/v4/chat/completions");

        post.addHeader("Content-Type","application/json");
        post.addHeader("Authorization"," Bearer 17d0043e95fee2405e508238fac3cad9.cOsSCV7D1WK3Ekaf");

        String paramJson="{\n" +
                "    \"model\": \"glm-4\",\n" +
                "    \"messages\": [\n" +
                "        {\n" +
                "            \"role\": \"user\",\n" +
                "            \"content\": \"介绍舟山\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json","UTF-8"));
        post.setEntity(stringEntity);

        CloseableHttpResponse response = httpClient.execute(post);
        if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String res = EntityUtils.toString(response.getEntity());
            System.out.println(res);
        }else{
            System.out.println(response.getStatusLine().getStatusCode());
        }

    }
}
